// pages/services/index.js
var app = getApp(), fetchApi = require("../../utils/httpPost.js"), common_Js = require("../../component/common.js");
import Toast from '../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    menus:[],
    loading: false,
    netWork: true,
    isLoading: true,
    netWork:false,
    hasData:false,
    service_type:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type){
      this.data.service_type = options.type;
    }
    this.onHttpInit();
  },
  goShowBuild(){
    Toast('工程师正加紧建设中...');
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/titleMenu/service', {
      menu_type:0,
      service_type: that.data.service_type,
    }, function (res) {
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          console.log("服务列表", res)
          that.setData({
            hasData: true,
            menus: res.data.data.menus
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
  
  scanCode: function (res) {
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var json_param = {}
          var linkParam = qrcontent;
          var device_sn = "";
          var qrtype = '';
          if(qrcontent.indexOf('https://') >= 0){
            var q = "?" + qrcontent.split("?")[1];
            var json_param = common_Js.getQueryVariable(q);
            var linkParam = "qrtype=mer&no=" + json_param.no;
            // that.merOp(json_param.no)
            if(qrcontent.indexOf('estate/info') >= 0){
             wx.navigateTo({ url: '/pages/estate/payyd/index?id=' + json_param.id})
            }else if(qrcontent.indexOf('shop/getEgg') >= 0){
              wx.navigateTo({ url: '/pagesMer/pages/payEgg/index?no=' + json_param.no})
            } else if(qrcontent.indexOf('mer/pay') >= 0){
              wx.navigateTo({ url: '/pages/pay/merOffline/index?no=' + json_param.no })
            } else if(qrcontent.indexOf('promoter/getInfo') >= 0){
              app.setStorageSync("promoter_id",json_param.pid);
              wx.switchTab({ url: '/pages/home/home' })
            }else{
              Toast.fail("当前二维码无效");
            }
          }else{
            Toast.fail("当前二维码无效");
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
})